package com.apes.framework.plugin.form.parser.node;

import com.apes.framework.plugin.filereader.xml.AbstractNodeParser;
import com.apes.framework.plugin.filereader.xml.ApesElement;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能：config 节点解析器
 *
 * @author xul
 * @create 2018-04-18 11:42
 */
@Component
public class FormNodeParser extends AbstractNodeParser {

    private static Map<String, Map> forms = new HashMap();

    /**
     * 可解析的节点列表
     **/
    private final String[] nodeNames = {"form"};

    /**
     * 可解析节点域
     **/
    public static final String domain = "form";


    /**
     * 功能：可解析节点域
     */
    @Override
    public String getDomain() {
        return domain;
    }

    /**
     * 功能：可解析的根节点列表
     */
    @Override
    public String[] getNames() {
        return nodeNames;
    }

    /**
     * 功能：解析节点
     *
     * @param node
     */
    @Override
    public ApesElement parse(Element node) {
        ApesElement element = null;
        try {
            element = super.parse(node);
            if (element == null) return null;
            forms.put(element.getAttribute("id"), element.parse());
        } catch (Exception e){
            logger.error(e);
        }
        return element;
    }

    public static Map getForm(String key) {
        Map form = forms.get(key);
        if (form == null) {
            throw new RuntimeException("表单： " + key + " 没有配置！");
        }
        return form;
    }

}
